<?php
class AuctionCacheList extends CRedisModel{
    public $scoreAttribute;
    public $indexAttribute;
    
    const AUCTION_LIST_PRE = 'auctionList';
    
    public function listName(){
        return '';
    }
    
    public function build($config){
        unset($config['class']);
        if (!empty($config)){
            foreach ($config as $property => $configValue){
                $this->{$property} = $configValue;
            }
        }
    }
    
    public function cacheKey(){
        return self::AUCTION_LIST_PRE.':'.$this->listName();
    }
    
    public function addAuctionItem($auctionCacheData){
        $this->addItem($auctionCacheData->{$this->scoreAttribute},$auctionCacheData->{$this->indexAttribute});
        return $this;
    }
    
    public function addItem($score,$item){
        $this->redis()->zAdd($this->cacheKey(),$score,$item);
        return $this;
    }
    
    public function addScore($pk,$score){
        $this->redis()->zIncrBy($this->cacheKey(),$score,$pk);
        return $this;
    }
    
    public function delItem($pk){
        $this->redis()->zRem($this->cacheKey(),$pk);
        $lastCount = $this->redis()->zRange($this->cacheKey(),0,-1);
        if ($lastCount <= 0){
            $this->redis()->delete($this->cacheKey());
        }
        return $this;
    }
    
    public function index(){
        return $this->redis()->zRange($this->cacheKey(),0,-1);
    }
}
